using System.Collections.Generic;
using System.Data;
using Magiq.Behaviors;
using Magiq.Update;

namespace Magiq.Databases.Update
{
    public class DatabaseUpdateBehavior<T> : DefaultBehavior, IDatabaseUpdateBehavior<T> where T : class
    {
        public IEnumerable<DatabaseUpdate<T>> BuildDatabaseUpdates()
        {
            return databaseAdapter.BuildDatabasUpdates(update);
        }

        public void Initialize(IDatabaseAdapter databaseAdapter, IUpdate<T> update)
        {
            this.update = update;
            this.databaseAdapter = databaseAdapter;
        }

        public IDbCommand BuildCommand(DatabaseUpdate<T> databaseUpdate)
        {
            var where = databaseUpdate.Source.Query();
            var builder = new UpdateCommandBuilder(databaseUpdate.DatabaseAdapter, where, databaseUpdate.ColumnAssignments);
            return builder.GetCommand();
        }

        private IUpdate<T> update;
        private IDatabaseAdapter databaseAdapter;
    }
}